package com.sena.controller;

import com.sena.base.BaseDateMessage;
import com.sena.domain.SenaSysUser;
import com.sena.enums.ResultCodeEnum;
import com.sena.exception.ThreadRunTimeException;
import com.sena.service.SenaSysUserService;
import com.sena.utils.AssertUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
@RequestMapping("/sys")
public class SenaSysUserController {


    @Autowired
    private SenaSysUserService senaSysUserService;

    /**
     * 用户登录
     *
     * @param senaSysUser
     * @param request
     * @return
     */
    @PostMapping(value = "/login")
    @ResponseBody
    public BaseDateMessage login(@RequestBody SenaSysUser senaSysUser, HttpServletRequest request) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        log.info("用户{}登录-----", senaSysUser.getmName());
        try {
            SenaSysUser ret = senaSysUserService.login(senaSysUser, request);
            AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.DATA_NOT_EXIST), ret);
            baseDateMessage.setData(ret);
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
        } catch (ThreadRunTimeException e) {
            log.info("处理错误");
            baseDateMessage.buildFail(e.getResultCodeEnum());
        } catch (Exception e) {
            log.info("服务器错误");
            baseDateMessage.buildFail(ResultCodeEnum.SERVER_ERROR);
        }
        return baseDateMessage;
    }


}
